/**
 * @ author: 汪孟苹
 */
define(["jquery","bootstrap","bbcare","comm/datatable/datatable"],function($,bootstrap,$bb,$datatable){
    var servers = {};
    var URL = $bb.baseUrl("followapp");
    var defaults = $bb.getPageParams();

    /**
     * 入参
     */
    var inParams = function(args){
        var params = $bb.getPageParams();
        // 年龄范围
        var ageFromValue = $.trim( $("#datatableForm input[name=ageFrom]").val() );
        var ageToValue = $.trim( $("#datatableForm input[name=ageTo]").val() );
        var ageFrom = ageFromValue != "" ? Number( ageFromValue ) * 12 : null;
        var ageTo = ageToValue != "" ? ( Number( ageToValue ) + 1 )  * 12 - 1 : null;
        // 孕周
        /*var preFromWeek = $("#datatableForm input[name=preFrom-week]").val();
        var preFromDay = $("#datatableForm input[name=preFrom-day]").val();
        var preToWeek = $("#datatableForm input[name=preTo-week]").val();
        var preToDay = $("#datatableForm input[name=preTo-day]").val();
        var preDayFrom, preDayTo;
        if( preFromWeek != "" || preFromDay != "" ){
            preDayFrom = parseInt(preFromWeek*7) + parseInt(preFromDay*1);
        }else{
            preDayFrom = null;
        }
        if( preToWeek != "" || preToDay !="" ){
            preDayTo = parseInt(preToWeek*7) + parseInt(preToDay*1);
        }else{
            preDayTo = null;
        }*/
        var $seqDom = $("#datatableForm select[name=seqFrom]").find("option:selected");
        var $seqTo = $("#datatableForm select[name=seqTo]").find("option:selected");
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "flag":"3",
                "busiTypeList":params.busiTypeList,
                "queueId":params.queueId,
                "schemeId":params.schemeId,
                "preDayStart":params.preDayStart,
                "preDayEnd":params.preDayEnd,
                "hisNo":$.trim( $("#datatableForm input[name=hisNo]").val() ),
                "userName":$.trim( $("#datatableForm input[name=userName]").val() ),
                "phoneNo":$.trim( $("#datatableForm input[name=phoneNo]").val() ),
                "ageFrom":ageFrom,
                "ageTo":ageTo,
                /*"preDayFrom":preDayFrom,
                "preDayTo":preDayTo,*/
                "seqFrom":$seqDom.attr("data-startDay"),
                "seqTo":$seqTo.attr("data-endDay"),
                "checkFrom":$("#datatableForm input[name=checkFrom]").val(),
                "checkTo":$("#datatableForm input[name=checkTo]").val(),
                "lostFrom":$("#datatableForm input[name=lostFrom]").val(),
                "lostTo":$("#datatableForm input[name=lostTo]").val(),
                "createAuthor":$("#datatableForm select[name=createAuthor]").val(),
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsHisNo = function(e){
        return e.hisNo ? e.hisNo : e.hisNo1 ? e.hisNo1 : e.hisNo2 ? e.hisNo2 : "-";
    }
    var columnsDoctorName = function(e){
        return e.doctor_name ? e.doctor_name : "患者自助";
    }
    var columnsData = function(){
        var columnsArr = [
            {"data":columnsHisNo},
            {"data":'userName'},
            {"data":'phoneNo'},
            {"data":'age'},
            {"data":'expectedDay'},
            {"data":'currNodeName'},
            {"data":columnsDoctorName},
            {
                "data":null,
                "className":"progress-interview"
            },
            {
                "data":null,
                "className":"status"
            },
            {
                "data": null,
                "className":"operate"
            }
        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        // 进度
        var h1 = "";
        var len = data.nodeStatusArr.length;
        var width = parseInt(100 / len) + '%';
        for (var i = 0; i < len; i++) {
            var status = data.nodeStatusArr[i];
            var obj = {};
            if( status == '0' ){                   // 未开始
                obj.className = "progress-notstart";
                obj.title = "未开始";
            }else if( status == '1' ){             // 已完成
                obj.className = "progress-finish";
                obj.title = "已完成";
            }else if( status == '2' ){             // 过期（未开始）
                obj.className = "progress-overdue";
                obj.title = "过期";
            }else if( status == '3' ){             // 过期（补填）
                obj.className = "progress-fill";
                obj.title = "补填";
            }else if( status == '4' ){             // 未完成（部分任务填写）
                obj.className = "progress-nofinish";
                obj.title = "未完成";
            }
            h1 += '<div style="width:'+width+'"><i class="'+obj.className+'" data-toggle="tooltip" data-placement="top" title="'+obj.title+'"></i></div>';
        }
        var html = '<div class="progress-wrap">'+ h1 +'</div>';
        $('td.progress-interview', row).html(html);
        // 状态
        var obj2 = {};
        if( data.isNodeStatus == "1" ){
            obj2.className = "fa-television color-red";
            obj2.title = "窗口期";
        }else if( data.isNodeStatus == "2" ){
            obj2.className = "fa-window-close-o color-green";
            obj2.title = "非窗口期";
        }else if( data.isNodeStatus == "3" ){
            obj2.className = "fa-clock-o color-orange";
            obj2.title = "临期";
        }else if( data.isNodeStatus == "4" ){
            obj2.className = "fa-chain-broken color-gray";
            obj2.title = "失访";
        }
        var h3 = '<div class="status-box"><i class="fa  '+ obj2.className +'" data-toggle="tooltip" data-placement="top" title="'+ obj2.title +'"></i></div>';
        $('td.status', row).html(h3);
        // 操作
        var h2 = '<div class="operate-box"><i class="fa fa-pencil-square-o color-blue" data-toggle="tooltip" data-placement="top" title="编辑"></i></div>';
        $('td.operate', row).html(h2);
        $("td.operate .operate-box i.color-blue",row).unbind("click");
        $("td.operate .operate-box i.color-blue",row).click(function(){
            var params = $bb.getPageParams();
            $bb.openPage("bch_user",{"busiTypeList":params.busiTypeList,"userId":data.userId});
        });
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        var id = $(".layout-bottom table").attr("id");
        $("#" + id).prev("div.row").remove();
        // 搜索 
        $("#serchBtn").click(function(){
            $datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatable.table.api().ajax.params();   //获取dt请求参数 
        });
        // 初始化所有工具提示
        $(function(){$("[data-toggle='tooltip']").tooltip();});
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "ctrl/ptmanage/qryUserListTable?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

    return servers;
});